﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Kcadastro.classes;

namespace Kcadastro.os
{
    public partial class BillRelat : System.Web.UI.Page
    {
        kcadDataContext db = new kcadDataContext(ConfigurationManager.ConnectionStrings["KcadConnectionString"].ToString());

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                carregar();
            }
        }

        private void carregar()
        {
            bt_carregar.Enabled = false;
            bill bil = new bill();
            dd_anos.DataSource = bil.verificaAnos();
            dd_anos.DataBind();

            if (dd_anos.Items.Count > 0) bt_carregar.Enabled = true;
        }

        protected void bt_carregar_Click(object sender, EventArgs e)
        {
            var res = (from tb in db.v_resumos
                           where tb.PK_IDemp == Convert.ToInt16(Session["IDemp"].ToString())
                           && tb.dt_reg.Value.Year == Convert.ToInt16(dd_anos.SelectedValue.ToString())
                           && tb.dt_reg.Value.Month == Convert.ToInt16(dd_mes.SelectedValue.ToString())
                           select tb);
            
            gv_lista.DataSource = res;
            gv_lista.DataBind();

            var rcuni = (from tb in db.tb_cunis
                           where tb.FK_IDemp == Convert.ToInt16(Session["IDemp"].ToString())
                           && tb.DataCuni.Value.Year == Convert.ToInt16(dd_anos.SelectedValue.ToString())
                           && tb.DataCuni.Value.Month == Convert.ToInt16(dd_mes.SelectedValue.ToString())
                            select tb).Count();
                         //{
                         //       PK_IDos = 0,
                         //       dt_reg = tb.DataCuni,
                         //       nomeOS = "Consultas Unitárias",
                         //       qtd_registros = tb.PK_IDcuni,
                         //       qtdprocessados = 
                         //  })
            lt_cunistt.Text = rcuni.ToString();
            if (res != null)
            {
                lt_ttproc.Text = (res.Sum(t => t.qtdprocessados) + (int) rcuni).ToString();
            }
            else
            {
                lt_ttproc.Text = "0";
            }
        }
    }
}